<!DOCTYPE html>
<html>

<head>

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">


    <title> - 任务列表</title>
    <meta name="keywords" content="">
    <meta name="description" content="">

    <link rel="shortcut icon" href="favicon.ico">
    <link href="css/bootstrap.min.css?v=3.3.6" rel="stylesheet">
    <link href="css/font-awesome.css?v=4.4.0" rel="stylesheet">

    <!-- Data Tables -->
    <link href="css/plugins/dataTables/dataTables.bootstrap.css" rel="stylesheet">

    <link href="css/animate.css" rel="stylesheet">
    <link href="css/style.css?v=4.1.0" rel="stylesheet">
    <link rel="stylesheet" href="css/plugins/jQueryUI/jquery-ui-1.10.4.custom.min.css">

</head>

<style>
    #retryTaskTable .dataTables_wrapper {
        position: relative;
        clear: both;
        zoom: 1;
        overflow-x: auto;
    }

    #retryTaskLogTable_wrapper {
        height: 90%;

    }

    .wrapper-content {
        padding: 10px !important;
    }

    #retryTaskTable retryTaskTable_wrapper {
        width: 800px;
    }

    .ui-dialog button {
        float: right;
    }

    #retry-task-log-dialog {
        height: 600px !important;
        overflow-y: auto;
        font-family: "open sans", "Helvetica Neue", Helvetica, Arial, sans-serif;
        font-size: 13px !important;
    }
</style>
<body class="gray-bg">
<div id="retry-task-log-dialog" style="display: none;">
    <input id="taskId" type="text" style="display: none;"/>
    <div class="wrapper wrapper-content animated fadeInRight">
        <div class="ibox float-e-margins">
            <div class="ibox-content">
                <table id="retryTaskLogTable"
                       class="table table-striped table-bordered table-hover project-list">
                </table>
            </div>
        </div>
    </div>
</div>
<div class="wrapper wrapper-content animated fadeInRight">
    <div class="row">

        <div class="col-sm-12">
            <div class="ibox float-e-margins">
                <div class="ibox-content">
                    <div class="layui-inline layui-show-xs-block" style="margin-bottom: 1%;">
                        <input type="text" id="projectName" placeholder="请输入项目名称" autocomplete="off"
                               class="layui-input"
                               style="width: 15%;display: block;float: left;margin-right: 10px;">
                        <input type="text" id="taskName" placeholder="请输入任务名称" autocomplete="off"
                               class="layui-input"
                               style="width: 15%;display: block;float: left;margin-right: 10px;">
                        <select id="status" style="width: 5%;float: left;margin-right: 10px;height: 25px;">
                            <option value="all">全部</option>
                            <option value="0">待执行</option>
                            <option value="1">执行中</option>
                            <option value="2">执行成功</option>
                            <option value="-1">执行失败</option>
                        </select>
                        <button class="layui-btn" lay-submit="" id="sreach"><i class="layui-icon">查找</i></button>
                    </div>
                    <table id="retryTaskTable" class="table table-striped table-bordered table-hover project-list">
                    </table>

                </div>
            </div>
        </div>
    </div>

</div>

<!-- 全局js -->
<script src="js/jquery.min.js?v=2.1.4"></script>
<script src="js/bootstrap.min.js?v=3.3.6"></script>

<script src="js/plugins"></script>
<!-- Data Tables -->
<script src="js/plugins/dataTables/jquery.dataTables.js"></script>
<script src="js/plugins/dataTables/dataTables.bootstrap.js"></script>
<script src="js/jquery-ui-1.10.4.min.js"/>
<!-- 自定义js -->
<script src="js/content.js?v=1.0.0"></script>

<script>
    let retryTaskTable;
    let retryTaskLogTable;
    let dialog;
    let editDialog;

    function loadTaskLog(data, callback, settings) {
        let pageNum = (data.start / data.length) + 1;
        let pageSize = data.length;
        var taskId = $("#taskId").val();
        if (taskId == null || taskId == "" || taskId == undefined) {
            return;
        }
        var url = "/retrytask/task/queryRetryTaskLog?pageNum=" + pageNum + "&pageSize=" + pageSize + "&taskId=" + taskId;
        $.ajax({
            url: url,
            type: "GET",
            data: {},
            dataType: "json",
            success: function (result) {
                if (result == null) {
                    return;
                }
                if (result.code == '-999') {
                    return;
                }
                var sucess = result.isSuccess;
                if (sucess == false) {
                    alert(result.errMsg);
                    result;
                }
                let dataArr = [];
                var returnData = {};
                if (result.data != null) {
                    var size = result.data.length;
                    for (var i = 0; i < size; i++) {
                        var obj = result.data[i];
                        var token = obj["token"];
                        var taskId = obj["id"];
                        var projectName = obj["projectName"];
                        var projectId = obj["id"];
                        obj["operate"] = "<td class='td-manage'>" +
                            "<a title='编辑' taskId='" + taskId + "' token=" + token + " projectId = " + projectId + " projectName=" + projectName + "  onclick='checkTaskLog(this)' href='javascript:;' style='padding-right: 10%;'>" +
                            "<i class='layui-icon'>查看执行日志</i>" +
                            "</a>" +
                            "<a title='删除' taskId='" + taskId + "'  token=" + token + " projectId = " + projectId + " projectName=" + projectName + "   onclick='checkTaskLog(this)' href='javascript:;'>" +
                            "<i class='layui-icon'>触发</i>" +
                            "</a>" +
                            "</td>";
                        dataArr.push(obj);
                    }
                }

                returnData.draw = data.draw;//这里直接自行返回了draw计数器,应该由后台返回
                returnData.recordsTotal = result.totalCount;
                returnData.recordsFiltered = result.totalCount;//后台不实现过滤功能，每次查询均视作全部结果
                returnData.data = dataArr;
                callback(returnData);
            },
            error: function (err) {
                console.log(err.statusText);
                console.log('异常');
            },
            complete: function (XMLHttpRequest, textStatus) {
                var redirect = XMLHttpRequest.getResponseHeader("Redirect");
                if (redirect == "true") {
                    window.parent.location.href = XMLHttpRequest.getResponseHeader("RedirectUrl");
                }
            }
        })
    }

    $(document).ready(function () {
        initTable();
        initLogTable();
        retryTaskTable = $('#retryTaskTable').dataTable();
        retryTaskLogTable = $("#retryTaskLogTable").dataTable();
    });

    dialog = $("#retry-task-log-dialog").dialog({
        autoOpen: false,
        height: "600px",
        width: "89%",
        modal: true,
        title: "查看日志",
        closeText: "X",
        buttons: []
    });
    $("#sreach").click(function () {
        initTable();
    });


    function loadProject(data, callback, settings) {
        let pageNum = (data.start / data.length) + 1;
        let pageSize = data.length;

        var url = "/retrytask/task/queryRetryTask?pageNum=" + pageNum + "&pageSize=" + pageSize;
        var projectName = $("#projectName").val();
        if (projectName != null && projectName != '' && projectName != undefined) {
            url = url + "&projectName=" + projectName;
        }
        var taskName = $("#taskName").val();
        if (taskName != null && taskName != '' && taskName != undefined) {
            url = url + "&taskName=" + taskName;
        }
        var status = $("#status").val();
        if (status != null && status != '' && status != undefined && status != 'all') {
            url = url + "&status=" + status;
        }

        $.ajax({
            url: url,
            type: "GET",
            data: {},
            dataType: "json",
            success: function (result) {
                if (result == null) {
                    return;
                }
                if (result.code == '-999') {
                    return;
                }
                var sucess = result.isSuccess;
                if (sucess == false) {
                    alert(result.errMsg);
                    result;
                }
                let dataArr = [];
                var returnData = {};
                if (result.data != null) {
                    var size = result.data.length;
                    for (var i = 0; i < size; i++) {
                        var obj = result.data[i];
                        var token = obj["token"];
                        var taskId = obj["id"];
                        var projectName = obj["projectName"];
                        var projectId = obj["id"];
                        obj["operate"] = "<td class='td-manage'>" +
                            "<a title='编辑' taskId=" + taskId + " token=" + token + " projectId = " + projectId + " projectName=" + projectName + "  onclick='checkTaskLog(this)' href='javascript:;' style='padding-right: 10%;'>" +
                            "<i class='layui-icon'>执行日志</i>" +
                            "</a>" +
                            "<a title='触发' taskId=" + taskId + " projectId = " + projectId + " projectName=" + projectName + "   onclick='triggerTask(this)' href='javascript:;'>" +
                            "<i class='layui-icon'>触发</i>" +
                            "</a>" +
                            "</td>";
                        dataArr.push(obj);
                    }
                }

                returnData.draw = data.draw;//这里直接自行返回了draw计数器,应该由后台返回
                returnData.recordsTotal = result.totalCount;
                returnData.recordsFiltered = result.totalCount;//后台不实现过滤功能，每次查询均视作全部结果
                returnData.data = dataArr;
                callback(returnData);
            },
            error: function (err) {
                console.log(err.statusText);
                console.log('异常');
            },
            complete: function (XMLHttpRequest, textStatus) {
                var redirect = XMLHttpRequest.getResponseHeader("Redirect");
                if (redirect == "true") {
                    window.parent.location.href = XMLHttpRequest.getResponseHeader("RedirectUrl");
                }
            }
        });
    }

    function checkTaskLog(e) {
        var taskId = $(e).attr("taskId");
        $("#taskId").val(taskId);
        initLogTable();
        dialog.dialog("open");
    }

    function triggerTask(e) {
        var taskId = $(e).attr("taskId");
        if (taskId == null || taskId == "" || taskId == undefined) {
            alert("taskId is not right");
            return
        }
        var url = "/retrytask/task/triggerTask?taskId=" + taskId;
        $.ajax({
            url: url,
            type: "GET",
            data: {},
            dataType: "json",
            success: function (result) {
                if (result == null) {
                    return;
                }
                if (result.code == '-999') {
                    return;
                }
                var sucess = result.isSuccess;
                if (sucess == false) {
                    alert(result.errMsg);
                    return;
                }
                alert("trigger sucess");
                initTable();
            },
            error: function (err) {
                console.log(err.statusText);
                console.log('异常');
            },
            complete: function (XMLHttpRequest, textStatus) {
                var redirect = XMLHttpRequest.getResponseHeader("Redirect");
                if (redirect == "true") {
                    window.parent.location.href = XMLHttpRequest.getResponseHeader("RedirectUrl");
                }
            }
        });
    }

    function initLogTable() {
        let columns = [
            {"data": "id", "orderable": false, "title": "执行id", "width": "60px"},
            {"data": "projectName", "orderable": false, "title": "项目名称", "width": "150px"},
            {"data": "taskName", "orderable": false, "title": "任务名称", "width": "80px"},
            {"data": "gmtCreate", "orderable": false, "title": "创建时间", "width": "80px"},
            {"data": "gmtModified", "orderable": false, "title": "修改时间", "width": "80px"},
            {"data": "preStartTime", "orderable": false, "title": "计划执行时间", "width": "80px"},
            {"data": "startTime", "orderable": false, "title": "实际执行时间", "width": "80px"},
            {"data": "endTime", "orderable": false, "title": "实际执行结束时间", "width": "80px"},
            {"data": "statusName", "orderable": false, "title": "执行状态", "width": "80px"},
            {"data": "executeIp", "orderable": false, "title": "执行机器ip", "width": "60px"},
            {"data": "errorMessage", "orderable": false, "title": "失败原因", "width": "150px"}
        ];
        return $('#retryTaskLogTable').dataTable({
            paging: true,
            pageLength: 10,
            iLeftColumns: 4,  //开启左侧两列固定
            bLengthChange: false,
            "bFilter": false,
            "destroy": true,
            "bPaginate": true,
            "pagingType": "full_numbers",
            serverSide: true,
            ajax: function (data, callback, settings) {
                loadTaskLog(data, callback, settings);
            },
            "oLanguage": { //国际化配置
                "sProcessing": "正在获取数据，请稍后...",
                "sLengthMenu": "显示 _MENU_ 条",
                "sZeroRecords": "没有您要搜索的内容",
                "sInfo": "从 _START_ 到  _END_ 条记录 总记录数为 _TOTAL_ 条",
                "sInfoEmpty": "记录数为0",
                "sInfoFiltered": "(全部记录数 _MAX_ 条)",
                "sInfoPostFix": "",
                "sSearch": "搜索",
                "sUrl": "",
                "oPaginate": {
                    "sFirst": "第一页",
                    "sPrevious": "上一页",
                    "sNext": "下一页",
                    "sLast": "最后一页"
                }
            },
            columns: columns
        });
    }

    function initTable() {
        let columns = [
            {"data": "id", "orderable": false, "title": "id", "width": "30px"},
            {"data": "projectName", "orderable": false, "title": "项目名称", "width": "150px"},
            {"data": "taskName", "orderable": false, "title": "任务名称", "width": "150px"},
            {"data": "taskDesc", "orderable": false, "title": "任务描述", "width": "150px"},
            {"data": "gmtCreate", "orderable": false, "title": "创建时间", "width": "150px"},
            {"data": "gmtModified", "orderable": false, "title": "修改时间", "width": "150px"},
            {"data": "statusName", "orderable": false, "title": "状态", "width": "90px"},
            {"data": "retryNum", "orderable": false, "title": "剩余次数", "width": "60px"},
            {"data": "originRetryNum", "orderable": false, "title": "重试次数", "width": "60px"},
            {"data": "nextPlanTime", "orderable": false, "title": "下次执行时间", "width": "150px"},
            {"data": "intervalSecond", "orderable": false, "title": "间隔时间(秒)", "width": "60px"},
            {"data": "corn", "orderable": false, "title": "corn", "width": "100px"},
            {"data": "taskType", "orderable": false, "title": "任务类型", "width": "100px"},
            {"data": "executeType", "orderable": false, "title": "执行类型", "width": "100px"},
            {"data": "delayTime", "orderable": false, "title": "初次延迟时间(秒)", "width": "100px"},
            {"data": "parameters", "orderable": false, "title": "参数", "width": "200px"},
            {"data": "operate", "orderable": false, "title": "操作", "width": "300px"}
        ];
        return $('#retryTaskTable').dataTable({
            paging: true,
            pageLength: 15,
            iLeftColumns: 4,  //开启左侧两列固定
            bLengthChange: false,
            "bFilter": false,
            "destroy": true,
            "bPaginate": true,
            "pagingType": "full_numbers",
            serverSide: true,
            ajax: function (data, callback, settings) {
                loadProject(data, callback, settings);
            },
            "oLanguage": { //国际化配置
                "sProcessing": "正在获取数据，请稍后...",
                "sLengthMenu": "显示 _MENU_ 条",
                "sZeroRecords": "没有您要搜索的内容",
                "sInfo": "从 _START_ 到  _END_ 条记录 总记录数为 _TOTAL_ 条",
                "sInfoEmpty": "记录数为0",
                "sInfoFiltered": "(全部记录数 _MAX_ 条)",
                "sInfoPostFix": "",
                "sSearch": "搜索",
                "sUrl": "",
                "oPaginate": {
                    "sFirst": "第一页",
                    "sPrevious": "上一页",
                    "sNext": "下一页",
                    "sLast": "最后一页"
                }
            },
            columns: columns
        });
    }
</script>


</body>

</html>
